﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace AndroidTools
{
    public class ApkHelper
    {
        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <returns></returns>
        public static List<Device> GetDeviceInfo()
        {
            string deviceStr = Helper.Cmd("adb devices",false);
            string[] tmp = Regex.Split(deviceStr, "\r\n");

            List<Device> devices = new List<Device>();
            for (int i = 1; i < tmp.Length; i++)
            {
                string[] s = tmp[i].Split('\t');
                if(s.Length < 2)
                {
                    continue;
                }
                Device device = new Device() { Name = s[0] };
                devices.Add(device);
            }

            return devices;
        }

        /// <summary>
        /// 获取目录列表
        /// </summary>
        /// <param name="device"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<File> GetPathList(string device,string path)
        {
            string deviceStr = ADB(device, string.Format(" ls -l {0}", path));

            //string deviceStr = Helper.Cmd(string.Format("adb -s {0} shell ls -l {1}", device, path),false);
            string[] tmp = Regex.Split(deviceStr, "\r\n");


            List<File> files = new List<File>();
            for (int i = 0; i < tmp.Length; i++)
            {
                string[] strs = tmp[i].Split(' ');
                if (strs.Length == 0)
                {
                    continue;
                }
                string type = strs[0];


                if (type == "" || (type.Substring(0, 1) != "-" && type.Substring(0, 1) != "d"))
                {
                    continue;
                }

                string name = strs[strs.Length - 1];

                files.Add(new File()
                {
                    IsDir = type.Substring(0, 1) == "d" ? true : false,
                    Name = name,
                    Path = path + "/" + name
                });
            }


            return files;
        }

        /// <summary>
        /// 查询进程占用信息
        /// </summary>
        /// <param name="device"></param>
        /// <param name="path"></param>
        /// <returns>进程PID</returns>
        public static string Netstat(string device, int port)
        {
            string res = ADB(device, "netstat -anp |grep "+ port);
            string[] tmp = Regex.Split(res, "\r\n");
            if (tmp.Length == 0)
            {
                return "";
            }

            tmp = tmp[0].Trim('\r').Split(' ');
            tmp = tmp[tmp.Length - 1].Trim(' ').Split('/');

            return tmp[0];
        }

        /// <summary>
        /// 结束进程
        /// </summary>
        /// <param name="device"></param>
        /// <param name="pid"></param>
        public static void Kill(string device, string pid)
        {
            ADB(device, "kill -9 " + pid);
        }

        /// <summary>
        /// 端口转发
        /// </summary>
        /// <param name="port"></param>
        public static void Forward(int port)
        {
            Helper.Cmd(string.Format("adb forward tcp:{0} tcp:{1}", port, port), false);
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="device"></param>
        /// <param name="cmd"></param>
        public static string ADB(string device, string cmd, bool isThread = false)
        {
            // string c = string.Format("adb -s {0} shell \"{1}\"", device, cmd.Trim());

            Helper.Cmd(string.Format("adb -s {0} root", device), false);
            return Helper.Cmd(string.Format("adb -s {0} shell \"{1}\"", device, cmd.Trim()), isThread);
        }



    }

    public class File
    {
        public bool IsDir { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
    }

    public class Device
    {
        public string Name { get; set; }
    }
}